package com.bionet.keggservice.service.impl;

import com.bionet.service_base.utils.Result;
import com.bionet.keggservice.entity.KeggLink;
import com.bionet.keggservice.mapper.KeggLinkMapper;
import com.bionet.keggservice.service.KeggLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hzx
 * @since 2021-06-24
 */
@Service
public class KeggLinkServiceImpl extends ServiceImpl<KeggLinkMapper, KeggLink> implements KeggLinkService {

    @Override
    public Result getAllList() {
        List<KeggLink> keggLinks = baseMapper.selectList(null);
        Map<String, Object> options = new HashMap<String, Object>();
        ArrayList<String> labels = new ArrayList<>();
        for (KeggLink entity : keggLinks) {
            if (!options.containsKey(entity.getOperation())) {
                options.put("value", entity.getOperation());
                options.put("label", entity.getOperation());
                if (entity.getDb() != null) {
                    Map<String, Object> Db = new HashMap<String, Object>();
                    if (labels.contains(entity.getDb())) {
                        ArrayList childrens = (ArrayList) options.get("children");
                        for (Object children : childrens) {
                            Map<String, Object> child = (Map<String, Object>) children;
                            String label = (String) child.get("label");
                            String db = entity.getDb();
                            if (label.equals(db)) {
                                Db = child;
                                childrens.remove(Db);
                                break;
                            }
                        }
                    } else {
                        Db.put("value", entity.getDb());
                        Db.put("label", entity.getDb());
                    }
                    if (!options.containsKey("children")) {
                        options.put("children", new ArrayList<String>());
                    }
                    ArrayList childrens = (ArrayList) options.get("children");
                    childrens.add(Db);
                    labels.add(entity.getDb());
                    options.put("children", childrens);
                }
            }
        }
        //给json加第三层的数据
        //获取children属性下的所有内容
        ArrayList optionsChildren = (ArrayList) options.get("children");
        //给optionsChildren内的每个属性都添加另一部分的数据库
        ArrayList newOptionsChildren = (ArrayList) optionsChildren.clone();
        //增加新属性entry
        Map<String, Object> Db = new HashMap<String, Object>();
        Db.put("value", "entry");
        Db.put("label", "entry");
        newOptionsChildren.add(Db);
        Map[] optionslist = new Map[1];
        optionslist[0] = options;
        return Result.ok().data("optionslist", optionslist).data("secondoptionslist",newOptionsChildren);
    }
}
